package com.commonly.commonlyUsed.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ApplicationDate extends Date{

    private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static Date parse(String dateStr, String pattern) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.parse(dateStr);
    }

    /**
     * 获取当前时间
     */
    public static ApplicationDate now() {
        return new ApplicationDate();
    }

    public ApplicationDate() {
    }

    public ApplicationDate(Date date) {
        this.setTime(date.getTime());
    }

    public ApplicationDate(String dateStr, String pattern) throws Exception {
        this.setTime(parse(dateStr, pattern).getTime());
    }

    public ApplicationDate(String dateStr) throws Exception {
        this.setTime(parse(dateStr, "yyyy-MM-dd HH:mm:ss").getTime());
    }

    public String toString() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(this);
    }

    public String toString(String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(this);
    }

    /**
     * 只获取日期
     * 响应:2021-04-27 00:00:00
     */
    public ApplicationDate getOnlyDate() throws Exception {
        return new ApplicationDate(parse(this.toString(), "yyyy-MM-dd"));
    }

    public boolean equal(Date date) {
        return this.getTime() == date.getTime();
    }

    public boolean equal(String dateStr) throws Exception {
        return this.getTime() == parse(dateStr, "yyyy-MM-dd HH:mm:ss").getTime();
    }

    public boolean isLess(Date date) {
        return this.getTime() < date.getTime();
    }

    public boolean isLess(String dateStr) throws Exception {
        return this.getTime() < parse(dateStr, "yyyy-MM-dd HH:mm:ss").getTime();
    }

    public boolean isLessOrEqual(Date date) {
        return this.getTime() <= date.getTime();
    }

    public boolean isLessOrEqual(String dateStr) throws Exception {
        return this.getTime() <= parse(dateStr, "yyyy-MM-dd HH:mm:ss").getTime();
    }

    public boolean isGreater(Date date) {
        return this.getTime() > date.getTime();
    }

    public boolean isGreater(String dateStr) throws Exception {
        return this.getTime() > parse(dateStr, "yyyy-MM-dd HH:mm:ss").getTime();
    }

    public boolean isGreaterOrEqual(Date date) {
        return this.getTime() >= date.getTime();
    }

    public boolean isGreaterOrEqual(String dateStr) throws Exception {
        return this.getTime() >= parse(dateStr, "yyyy-MM-dd HH:mm:ss").getTime();
    }

    public ApplicationDate addYears(int years) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(this);
        cal.add(1, years);
        this.setTime(cal.getTimeInMillis());
        return this;
    }

    public String getDossenYear() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(this);
        int year = cal.get(1);
        return String.valueOf(year);
    }

    public ApplicationDate addMonths(int months) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(this);
        cal.add(2, months);
        this.setTime(cal.getTimeInMillis());
        return this;
    }

    public int getDossenMonth() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(this);
        int month = cal.get(2);
        return month + 1;
    }

    public ApplicationDate addDays(long days) {
        this.setTime(this.getTime() + days * 24L * 60L * 60L * 1000L);
        return this;
    }

    public int getDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(this);
        int day = cal.get(6);
        return day;
    }

    public int getDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(this);
        int day = cal.get(5);
        return day;
    }

    public int getDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(this);
        int day = cal.get(7);
        return day == 1 ? 7 : day - 1;
    }

    public ApplicationDate addHours(long hours) {
        this.setTime(this.getTime() + hours * 60L * 60L * 1000L);
        return this;
    }

    public int getHourOfDay() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(this);
        int day = cal.get(11);
        return day;
    }

    public ApplicationDate addMinutes(long minutes) {
        this.setTime(this.getTime() + minutes * 60L * 1000L);
        return this;
    }

    public ApplicationDate addSeconds(long seconds) {
        this.setTime(this.getTime() + seconds * 1000L);
        return this;
    }
}
